﻿<#
.SYNOPSIS  
    Mounts and upgrades SharePoint 2007 content databases. 
.DESCRIPTION  
    Mounts and upgrades SharePoint 2007 content databases.
.NOTES
    If the content database is already mounted, an error will occur.  Check upgrade log files for content database issues. 
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Adding content databases ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

foreach($webApp in $config.Settings.WebApplications.WebApplication)
{
	foreach($contentDb in $webApp.ContentDatabases.ContentDatabase)
	{
		if($contentDb -ne $null)
		{
			$issues = Test-SPContentDatabase -Name $contentDb -WebApplication $webApp.Url -ErrorAction Stop
			if($issues.Count -gt 0) 
			{
				Write-Warning ("      There are {0} issues with the content database '{1}'." -f $issues.Count.ToString(), $contentDb)
				foreach($issue in $issues)
				{
					Write-Warning ("      " + $issue.Message)
				}
			}
			
			Write-Host ("         Upgrading '{0}' ({1})..." -f $contentDb, [DateTime]::Now.ToString())
			$db = Mount-SPContentDatabase -Name $contentDb -WebApplication $webApp.Url
			Write-Host ("         Done upgrading '{0}' ({1})" -f $contentDb, [DateTime]::Now.ToString())

            #TODO: parallel database attach
			#$job = Start-Job -Name ("Upgrade_" + "$contentDb") -InputObject $contentDb,$webApp.Url -InitializationScript {Add-PsSnapin  Microsoft.SharePoint.PowerShell} -ScriptBlock { foreach($jobInfo in $input) { $db = Mount-SPContentDatabase -Name $jobInfo[0] -WebApplication $jobInfo[1] } }
			#Write-Host ("      Started upgrade job '{0}' ({1})." -f $job.Name, [DateTime]::Now.ToString())
		}
	}
}

#Write-Host ("      Waiting for all content database upgrade jobs to complete...")

#Wait-Job *
#Receive-Job *
#Remove-Job *

Write-Host ("   Done adding content databases ({0})" -f [DateTime]::Now.ToString())